const moment = require('moment')
const ProductCategory = require('../../model/ProductCategory')
const Product = require('../../model/Product')
const ProductSub = require('../../model/ProductSub')

module.exports = {

    //获取日志列表
    changeProduct: async(req, res, next) => {
        try {
            if (req.body.id) {
                console.log('change')
                let product = await Product.findOne({ _id: req.body.id, isDelete: 0 }).exec()
                let productSub = await ProductSub.findOne({ _id: product.attribute[0], isDelete: 0 }).exec()
                product.name = req.body.name,
                    product.description = req.body.description,
                    product.detail = req.body.detail,
                    product.productCategory = req.body.productCategory,
                    product.status = req.body.status,
                    product.image = req.body.imageUrl,
                    product.discountPrice = req.body.discountPrice,
                    product.discountStatus = req.body.discountStatus,
                    product.swipeImage = req.body.swipeImage
                    await product.save()

                // productSub.attributes = req.body.attributes,
                //     productSub.price = req.body.price,
                //     productSub.point = req.body.point,
                //     productSub.stock = req.body.stock,
                //     productSub.isDelete = req.body.isDelete,
                //     productSub.discountPrice = req.body.discountPrice,
                //     productSub.discountStatus = req.body.discountStatus
                // await productSub.save()

                // let result = {}
                // result.product = product
                // result.productSub = productSub
                return res.ok('修改产品成功', product)
            } else if (!req.body.id) {
                let newProduct = new Product({
                        name: req.body.name,
                        description: req.body.description,
                        detail: req.body.detail,
                        productCategory: req.body.productCategory,
                        image: req.body.imageUrl,
                        status: 0,
                        isDelete: 0,
                        discountPrice: req.body.discountPrice,
                        discountStatus: req.body.discountStatus,
                        swipeImage : req.body.swipeImage
                    })
                    // let newProductSub = new ProductSub({
                    //     attributes: req.body.attributes,
                    //     product: newProduct._id,
                    //     price: req.body.price,
                    //     point: req.body.point,
                    //     stock: req.body.stock,
                    //     isDelete: 0,
                    //     discountPrice: req.body.discountPrice,
                    //     discountStatus: req.body.discountStatus,
                    // })
                    // await newProductSub.save()
                    // newProduct.attribute = newProductSub._id
                await newProduct.save()
                    // let result = {}
                    // result.product = newProduct
                    // result.productSub = newProductSub
                return res.ok('创建产品成功', newProduct)
            }
        } catch (e) {
            next(e)
        }
    },


    deleteProduct: async(req, res, next) => {
        try {
            let product = await Product.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            product.isDelete = 1
            await product.save()
            return res.ok('删除成功', product)
        } catch (e) {
            next(e)
        }
    },


    productOnShelf: async(req, res, next) => {
        try {
            let product = await Product.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            product.status = 1
            await product.save()
            return res.ok('上线成功', product)
        } catch (e) {
            next(e)
        }
    },


    productOffShelf: async(req, res, next) => {
        try {
            let product = await Product.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            product.status = 0
            await product.save()
            return res.ok('下线成功', product)
        } catch (e) {
            next(e)
        }
    },
    getOneProduct: async(req, res, next) => {
        let product = await Product.findOne({ _id: req.body.id, isDelete: 0 }).populate('attribute productCategory').exec()
        return res.ok('获取成功', product)
    },


    getProductList: async(req, res, next) => {
        try {
            let option = {}
            option.isDelete = 0
            if (req.body.name) {
                option.name = new RegExp(req.body.name, 'i')
            }
            if (req.body.productCategory) {
                option.productCategory = req.body.productCategory
            }
            if (req.body.isDelete) {
                option.isDelete = req.body.isDelete
            }
            if (req.body.status) {
                option.status = req.body.status
            }
            let count = await Product.countDocuments(option).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let data = await Product.find(option).skip((page - 1) * pageSize).limit(pageSize).populate('productCategory attribute').exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.data = data
            result.pageSize = pageSize
            return res.ok('获取列表成功', result)
        } catch (e) {
            next(e)
        }
    },

    getProductCategoryList: async(req, res, next) => {
        try {
            let option = {}
            option.isDelete = 0
            if (req.body.name) {
                option.name = new RegExp(req.body.name, 'i')
            }
            let count = await ProductCategory.countDocuments(option).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let data = await ProductCategory.find(option).skip((page - 1) * pageSize).limit(pageSize).exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.data = data
            result.pageSize = pageSize
            return res.ok('获取列表成功', result)
        } catch (e) {
            next(e)
        }
    },

    getOneProductCategory: async(req, res, next) => {
        let productCategory = await ProductCategory.findOne({ _id: req.body.id, isDelete: 0 }).exec()
        return res.ok('获取成功', productCategory)
    },


    changeProductCategory: async(req, res, next) => {
        try {
            if (req.body.id) {
                // console.log('change')
                let productCategory = await ProductCategory.findOne({ _id: req.body.id, isDelete: 0 }).exec()
                productCategory.name = req.body.name,
                    productCategory.description = req.body.description,
                    productCategory.image = req.body.image, // links of cdn
                    await productCategory.save()
                return res.ok('修改成功', productCategory)
            } else if (!req.body.id) {
                // console.log('add')
                let newProductCategory = new ProductCategory({
                    name: req.body.name,
                    description: req.body.description,
                    image: req.body.image, // links of cdn
                    isDelete: 0,
                })
                await newProductCategory.save()
                return res.ok('创建成功', newProductCategory)
            }
        } catch (e) {
            next(e)
        }
    },


    deleteproductCategory: async(req, res, next) => {
        try {
            let productCategory = await ProductCategory.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            productCategory.isDelete = 1
            await productCategory.save()
            return res.ok('删除成功', productCategory)
        } catch (e) {
            next(e)
        }
    },


    changeProductSub: async(req, res, next) => {
        try {
            if (req.body.id) {
                let productSub = await ProductSub.findOne({ _id: req.body.id, isDelete: 0 }).exec()
                productSub.attributes = req.body.attributes
                productSub.product = req.body.product
                productSub.price = req.body.price
                productSub.point = req.body.point
                productSub.stock = req.body.stock
                productSub.discountPrice = req.body.discountPrice
                productSub.discountStatus = req.body.discountStatus
                await productSub.save()
                return res.ok('修改成功', productSub)
            } else if (!req.body.id) {
                let newProductSub = new ProductSub({
                    attributes: req.body.attributes,
                    product: req.body.product,
                    price: req.body.price,
                    point: req.body.point,
                    stock: req.body.stock,
                    discountPrice: req.body.discountPrice,
                    discountStatus: req.body.discountStatus,
                    isDelete: 0
                })
                await newProductSub.save()
                let product = await Product.findOne({ _id: req.body.product, isDelete: 0 }).exec()
                product.attribute.push(newProductSub._id)
                await product.save()
                return res.ok('创建成功', newProductSub)
            }
        } catch (e) {
            next(e)
        }
    },


    deleteProductSub: async(req, res, next) => {
        try {
            console.log('test')
            let productSub = await ProductSub.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            productSub.isDelete = 1
            await productSub.save()
            let product = await Product.findOne({ attribute: { $in: [req.body.id] }, isDelete: 0 }).exec()
            let index = product.attribute.indexOf(req.body.id)
            product.attribute.splice(index, 1)
            product.markModified("productSub")
            await product.save()
            return res.ok('删除成功')
        } catch (e) {
            next(e)
        }
    },



    getProductSubList: async(req, res, next) => {
        try {
            let option = {}
            option.isDelete = 0
            if (req.body.name) {
                option.name = new RegExp(req.body.name, 'i')
            }
            if (req.body.product) {
                option.product = req.body.product
            }
            option.isDelete = 0
            let count = await ProductSub.countDocuments(option).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let data = await ProductSub.find(option).skip((page - 1) * pageSize).limit(pageSize).populate('product').exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.data = data
            result.pageSize = pageSize
            return res.ok('获取列表成功', result)
        } catch (e) {
            next(e)
        }
    },

    getOneProductSub: async(req, res, next) => {
        let productSub = await ProductSub.findOne({ _id: req.body.id, isDelete: 0 }).exec()
        return res.ok('获取成功', productSub)
    },


};